import { Fun1 } from "../type/fun";
import { KVAny } from "../type/kv";

export const filterList = <T>(list: T[], filterFun?: Fun1<boolean>): T[] =>
  filterFun ? list.filter(item => filterFun(item)) : list;

export const filterKV = (object: KVAny, filterFun?: Fun1<boolean>): KVAny => {
  if (filterFun) {
    const result: KVAny = {};
    Object.entries(object).forEach(entry => {
      if (filterFun(entry)) {
        result[entry[0]] = entry[1];
      }
    });
    return result;
  }

  return object;
};
